package com.situ.mall.web.controller;


import com.situ.mall.common.entity.Order;
import com.situ.mall.common.exception.MyException;
import com.situ.mall.common.mapper.OrdersMapper;
import com.situ.mall.common.service.IOrdersService;
import com.situ.mall.common.util.StringUtil;
import com.situ.mall.common.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-01-29
 */
@RestController
@RequestMapping("/client")
public class OrdersController {

    @Autowired
    private IOrdersService orderService;

    @Autowired
    private OrdersMapper orderMapper;

    //生成订单
    @PostMapping("/order/order")
    public ResultVO order(Integer orderTypeId, Integer memberId, String memberTel, String memberName, String memberDetail, String clinicTime, String hospital, @RequestParam(required = false) Integer workerId, String price) {
        // 把字符串转换为时间类
        LocalDate time = StringUtil.localDate(clinicTime);
        return orderService.order(orderTypeId, memberId, memberTel, memberName, memberDetail, time, hospital, workerId, price);
    }

    //支付
    @PostMapping("/order/pay")
    public ResultVO pay(Integer orderId, String money) {
        Order order = orderService.getById(orderId);
        //先判断订单是否存在
        if (StringUtil.isObjectNotEmpty(order)) {
            //不允许重复支付
            if (money == null || money.isEmpty()) {
                throw new MyException("支付错误，请重试");
            } else if (Objects.equals(order.getStatus(), "0")) {
                throw new MyException("请勿重复支付");
            } else {
                BigDecimal money1 = new BigDecimal(money);
                String status = "0";

                //有值的更新，没有值的不更新
                Order newOrder = new Order();
                newOrder.setId(orderId);
                newOrder.setMoney(money1);
                newOrder.setStatus(status);

                orderService.updateById(newOrder);

                //再查一遍，返回
                return ResultVO.data(200, "支付成功", orderMapper.selectById(orderId));
            }
        } else {
            throw new MyException("订单不存在！");
        }
    }

    //接单
    @PostMapping("/order/receiveOrder")
    public ResultVO receiveOrder(Integer orderId, Integer workerId) {
        // 先去查这个订单有没有被接单
        if (!Objects.equals(orderMapper.selectById(orderId).getStatus(), "0")) {
            throw new MyException("该订单已被接单");
        } else {
            orderMapper.receiveOrder(orderId, workerId);
        }
        return ResultVO.data(200, "接单成功", orderMapper.list(null, null, null, null, "0"));
    }

    //开始服务（更新订单状态）
    @PostMapping("/order/startOrder")
    public ResultVO startOrder(Integer orderId, String status) {
        Order order = new Order();
        order.setId(orderId);
        order.setStatus(status);
        if (Objects.equals(status, "4")) {
            order.setFinishTime(LocalDateTime.now());
        }
        orderMapper.updateById(order);
        return ResultVO.data(200, "请求成功", orderMapper.list(null, null, orderId, null, null));
    }

    //完成服务（更新订单状态、病人情况、医嘱）
    @PostMapping("/order/finishOrder")
    public ResultVO finishOrder(Integer orderId, String situation, String suggestion) {
        Order order = new Order();
        order.setId(orderId);
        order.setStatus("3");
        order.setSituation(situation);
        order.setSuggestion(suggestion);
        orderMapper.updateById(order);
        return ResultVO.data(200, "请求成功", orderMapper.list(null, null, orderId, null, null));
    }

    //完成评价（更新用户、陪诊员评价）
    @PostMapping("/order/finishEvaluate")
    public ResultVO finishEvaluate(Integer orderId, Integer eva, String type) {
        System.out.println(orderId + " " + eva + " " + type);
        Order order = new Order();
        order.setId(orderId);
        if (Objects.equals(type, "1")) {
            order.setMemberEva(eva);
        } else {
            order.setWorkerEva(eva);
        }
        orderMapper.updateById(order);
        return ResultVO.data(200, "请求成功", orderMapper.list(null, null, orderId, null, null));

    }

    //根据订单id获取订单详细信息
    @GetMapping("/order/getOrder")
    public ResultVO getOrder(Integer orderId) {
        Order order = orderService.getById(orderId);
        //先判断有没有这个订单，没有就报错
        if (StringUtil.isObjectNotEmpty(order)) {
            return ResultVO.data(200, "请求成功", orderMapper.list(null, null, orderId, null, null));
        } else {
            throw new MyException("订单不存在！");
        }
    }

    //根据 用户id & 订单状态 获取订单列表
    @GetMapping("/order/orderList")
    public ResultVO orderList(Integer memberId, String status) {
        if (memberId == null) {
            throw new MyException("请先登陆！");
        } else {
            return ResultVO.data(200, "请求成功", orderMapper.list(null, null, null, memberId, status));
        }
    }

    //根据 陪诊员id & 订单状态 获取订单列表
    @GetMapping("/order/workerOrderList")
    public ResultVO workerOrderList(Integer workerId, String status) {
        if (workerId == null) {
            throw new MyException("请先登录！");
        } else {
            return ResultVO.data(200, "请求成功", orderMapper.workerOrderList(workerId, status));
        }
    }

    //查未被接单的所有订单
    @GetMapping("/order/allOrders")
    public ResultVO allOrders() {
        return ResultVO.data(200, "请求成功", orderMapper.list(null, null, null, null, "0"));
    }

}
